package com.ruoyi.system.job;

import com.ruoyi.system.domain.Task;
import com.ruoyi.system.domain.TaskLine;
import com.ruoyi.system.domain.TaskUserMark;
import com.ruoyi.system.mapper.TaskLineMapper;
import com.ruoyi.system.mapper.TaskUserMarkMapper;
import com.ruoyi.system.service.ITaskLineService;
import com.ruoyi.system.service.ITaskService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 平均分计算任务
 */
@Component
public class TaskCalculateJob {

    @Resource
    private ITaskService taskService;

    @Resource
    private TaskLineMapper taskLineMapper;

    @Resource
    private TaskUserMarkMapper taskUserMarkMapper;

    private static final Map<Long, Long> FLAG_MAP = new HashMap<>();
    private static final Map<Long, String> GRADE_MAP = new HashMap<>();

    static {
        for (int i = 0; i <= 40; i++) {
            FLAG_MAP.put((long) i, 40L);
            GRADE_MAP.put((long) i, "不称职");
        }
        for (int i = 41; i <= 60; i++) {
            FLAG_MAP.put((long) i, 60L);
            GRADE_MAP.put((long) i, "基本称职");
        }
        for (int i = 61; i <= 80; i++) {
            FLAG_MAP.put((long) i, 80L);
            GRADE_MAP.put((long) i, "称职");
        }
        for (int i = 81; i <= 100; i++) {
            FLAG_MAP.put((long) i, 100L);
            GRADE_MAP.put((long) i, "优秀");
        }
    }

    public void calculate() {
        System.out.println("定时任务开始执行");
        // 获取所有的测评任务
        List<Task> tasks = taskService.selectTaskList(new Task());
        // 开始计算
        tasks.parallelStream().forEach(task -> {
            // 获取所有的明细
            TaskLine taskLine = new TaskLine();
            taskLine.setTaskId(task.getId());
            List<TaskLine> taskLines = taskLineMapper.selectTaskLineList(taskLine);
            // 计算评分
            taskLines.forEach(line -> {
                TaskUserMark taskUserMark = new TaskUserMark();
                taskUserMark.setUserId(line.getUserId());
                taskUserMark.setTaskId(line.getTaskId());
                // 统计得分
                List<TaskUserMark> taskUserMarks = taskUserMarkMapper.selectTaskUserMarkList(taskUserMark);
                // 如果有人打分了 才可以
                if (!CollectionUtils.isEmpty(taskUserMarks)) {
                    // 总分
                    List<BigDecimal> roleGradeList = new ArrayList<>();
                    // 计算平均分
                    taskUserMarks.stream().collect(Collectors.groupingBy(TaskUserMark::getJudgmentRole)).forEach((role, list) -> {
                        int size = list.size();
                        BigDecimal proportion = null;
                        if (role.equals("学校领导")) {
                            proportion = new BigDecimal("0.4");
                        }
                        if (role.equals("中层干部")) {
                            proportion = new BigDecimal("0.3");
                        }
                        if (role.equals("各部门、分院教职工")) {
                            proportion = new BigDecimal("0.3");
                        }
                        // 计算出来的总分
                        BigDecimal sumGrade = new BigDecimal(0);
                        for (TaskUserMark item : list) {
                            // 当前分数的得分
                            Long grade = FLAG_MAP.get(item.getGrade());
                            sumGrade = sumGrade.add(new BigDecimal(grade));
                        }
                        BigDecimal mean = sumGrade.multiply(proportion);
                        // 总分做一个去平均
                        BigDecimal divide = mean.divide(new BigDecimal(size), 0, RoundingMode.DOWN);
                        // 添加到总的数值中
                        roleGradeList.add(divide);
                    });

                    if (!CollectionUtils.isEmpty(roleGradeList)) {
                        // 总数
                        BigDecimal xxx = new BigDecimal(0);
                        for (BigDecimal bigDecimal : roleGradeList) {
                            xxx = xxx.add(bigDecimal);
                        }
                        // 平均分
                        long averageScore = xxx.longValue();
                        // 等级
                        String register = GRADE_MAP.get(averageScore);

                        line.setAverageScore(averageScore);
                        line.setRegister(register);
                        taskLineMapper.updateTaskLine(line);
                    }
                }
            });
        });
        System.out.println("定时任务结束");
    }

}
